﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using BYFW.DAL;
using LDSP.Helper;

namespace LDSP.UI.Admin
{
    public partial class MngChemicalCatalog : LDSP.BRL.BaseValidUserPage
    {
        private int col_name = 2;
        private int col_summary = 3;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.BindGrid();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        public override void RedirectBefore()
        {
            //如果不是系统里合法的用户，则退出到登陆页面。
            if (LoginUser == null && LoginAdmin == null)
            {
                this.GoToLogin();
            }
            else if (LoginUser != null)
            {
                //用户被删除后，即使已登录了也退出
                LoginUser.GetProperties(LoginUser.ID);
                if (LoginUser.ID == 0)
                {
                    this.GoToLogin();
                }
            }
        }

        public bool IsAdmin
        {
            get
            {
                return LoginAdmin != null;
            }
        }

        protected string EmptySearchStr
        {
            get { return this.GetVSString("EmptySearchStr_ChemicalCatalog"); }
            set { this.SetVS("EmptySearchStr_ChemicalCatalog", value); }
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "name";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return true;
        }
        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.SetSpecialPageInfo();
            BindDDLPageSize();

            EmptySearchStr = Constants.EmptySearch.Chemical_Catalog;
            UIHelper.SetDefaultFilterCondition(this.txt_name, EmptySearchStr);

            //企业管理员及普通用户，不能添加修改删除.
            //系统管理员不需要查看详细。
            this.btn_add.Visible = this.LoginAdmin != null;
            this.btn_delete.Visible = this.LoginAdmin != null;
            this.grd_list.Columns[0].Visible = this.LoginAdmin != null;
            this.grd_list.Columns[4].Visible = this.LoginAdmin != null;
            this.grd_list.Columns[5].Visible = this.LoginAdmin != null;
            this.grd_list.Columns[6].Visible = false;//不显示“详细”这一列
        }
        protected void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pageSize);
            this.ddl_pageSize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void BindGrid()
        {
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new ChemicalCatalog().GetRecords(g_pageInfo.ConditionWhere, true), g_pageInfo.GetSortExpression());

            UIHelper.BindGridWithPageInfo(grd_list, g_pageInfo, dt);

            SavePageSizeSetting();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }

        protected void GetCondition()
        {
            string nameCond = this.txt_name.Text.Trim();
            string cond = "";

            if (nameCond != ""
                && nameCond != EmptySearchStr)
            {
                cond = this.AppendConditionMulti(cond, nameCond, "name", "summary");
            }
            else
            {
                nameCond = "";
            }

            this.SetHighlight(nameCond);

            g_pageInfo.Condition = cond;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.JSRedirect("../Admin/AddChemicalCatalog.aspx", false);
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.DoGirdChecked<ChemicalCatalog>(this.grd_list, "chb", delegate(ChemicalCatalog ett) {
                    new Chemical().DeleteByChemicalCatalogId(ett.ID);
                    ett.Delete();
                });

                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                if (e.CommandName == "updateItem")
                {
                    this.JSRedirect("../Admin/AddChemicalCatalog.aspx?id=" + id, false);
                }
                if (e.CommandName == "deleteItem")
                {
                    new Chemical().DeleteByChemicalCatalogId(id);

                    ChemicalCatalog ett = new ChemicalCatalog();
                    ett.GetProperties(id);
                    ett.Delete();
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var ett = UIHelper.GetEntityFromGridViewRow<ChemicalCatalog>(e.Row);

                    this.ShowSequenceNumber(e.Row, 1);

                    //如果是企业用户，隐藏"修改"和"删除"按钮
                    if (LoginAdmin == null)
                    {
                        WebControl lnk_Modify = (WebControl)e.Row.FindControl("LinkButton2");
                        WebControl lnk_Delete = (WebControl)e.Row.FindControl("LinkButton3");
                        lnk_Modify.Visible = false;
                        lnk_Delete.Visible = false;
                    }

                    //获取快照。
                    string menu = this.GetSnapshot(ett.Summary);
                    menu = UIHelper.AppendLink(menu, LDSP.Helper.LongTextInstanceNames.ChemicalCatalog, Server.UrlEncode(this.GetHighlight()), ett.ID);
                    e.Row.Cells[col_summary].Text = !string.IsNullOrEmpty(menu) ? menu.Replace("\r\n", "<br/>") : string.Empty;

                    UIHelper.AppendEmptyLine(e.Row, col_summary);
                    this.HighlightMulti(e.Row, col_summary, col_name);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pageSize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}